package com.ruoyi.web.controller.system.biz;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.system.domain.Assess;
import com.ruoyi.system.domain.AssessTable;
import com.ruoyi.system.domain.dto.AssessTableDTO;
import com.ruoyi.system.domain.dto.AssessTableListDTO;
import com.ruoyi.system.service.ISysRoleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.IAssessTableService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import static com.ruoyi.common.utils.SecurityUtils.getLoginUser;

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2025-04-22
 */
@RestController
@RequestMapping("/system/table")
public class AssessTableController extends BaseController {
    @Autowired
    private IAssessTableService assessTableService;
    @Autowired
    private ISysRoleService roleService;

    /**
     * 查询【请填写功能名称】列表
     */
    @GetMapping("/list")
    public TableDataInfo list(AssessTable assessTable) {
        startPage();
        LoginUser loginUser = getLoginUser();
        List<SysRole> sysRoles = roleService.selectRoleByUserId(loginUser.getUserId());
        List<Long> roleLists = null;
        if (!loginUser.getUser().isAdmin()) {
            for (SysRole sysRole : sysRoles) {
                roleLists = new ArrayList<>();
                roleLists.add(sysRole.getRoleId());
                roleLists.add(1L);
            }
        }

        assessTable.setRoleLists(roleLists);
        List<AssessTable> list = assessTableService.selectAssessTableList(assessTable);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:table:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AssessTable assessTable) {
        List<AssessTable> list = assessTableService.selectAssessTableList(assessTable);
        ExcelUtil<AssessTable> util = new ExcelUtil<AssessTable>(AssessTable.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /* * 导出【请填写功能名称】列表
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/import")
    public void importTable(@RequestParam("file") MultipartFile file) {
        LoginUser loginUser = getLoginUser();
        assessTableService.importTable(file, loginUser);
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(assessTableService.selectAssessTableById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AssessTable assessTable) {
        LoginUser loginUser = getLoginUser();
        List<SysRole> sysRoles = roleService.selectRoleByUserId(loginUser.getUserId());
        if (!sysRoles.isEmpty()) {
            assessTable.setRoleId(sysRoles.get(0).getRoleId());
        } else {
            throw new ServiceException("未找到用户[" + loginUser.getUsername() + "]的角色");
        }
        assessTable.setAdminType(0);
        if (loginUser.getUser().isAdmin()) {
            assessTable.setAdminType(1);
        }
        return toAjax(assessTableService.insertAssessTable(assessTable));
    }

    /**
     * 修改【请填写功能名称】
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AssessTable assessTable) {
        return toAjax(assessTableService.updateAssessTable(assessTable));
    }

    /**
     * 删除【请填写功能名称】
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(assessTableService.deleteAssessTableByIds(ids));
    }


    /**
     * 查询【请填写功能名称】列表
     */
    @GetMapping("/query/list")
    public AjaxResult queryList(AssessTableListDTO assessTable) {
        List<AssessTable> list = assessTableService.selectAssessTableQueryList(assessTable);
        return success(list);
    }
}
